package com.pengcheng.taskapplication.mvp.view.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.pengcheng.taskapplication.R;
import com.pengcheng.taskapplication.adapter.CarAdapter;
import com.pengcheng.taskapplication.adapter.ParkAdapter;
import com.pengcheng.taskapplication.contants.ConstantValue;
import com.pengcheng.taskapplication.model.CarModel;
import com.pengcheng.taskapplication.model.ParkOrderModel;
import com.pengcheng.taskapplication.model.SubscribeOrderListModel;
import com.pengcheng.taskapplication.model.UrlItemModel;
import com.pengcheng.taskapplication.mvp.presenter.UserPresenter;
import com.pengcheng.taskapplication.mvp.presenter.UserPresenterV2;
import com.pengcheng.taskapplication.mvp.view.activity.ConfrimOrderActivity;
import com.pengcheng.taskapplication.mvp.view.activity.Html5Activity;
import com.pengcheng.taskapplication.net.IView;
import com.pengcheng.taskapplication.util.SPUtils;

import java.lang.reflect.Type;
import java.util.ArrayList;

import butterknife.BindView;

public class MyCarListFragment extends BaseFragment implements IView{
    private UserPresenter userPresenter;
    @BindView(R.id.pull_to_refresh_lv)
    PullToRefreshListView pullToRefreshListView;
    ArrayList<CarModel> list =new ArrayList<>();
    private CarAdapter adapter;

    private int page=1;
    private int pagesize=10;
    private String uid;
    private String newtoken;
    private boolean showAll;

    @Override
    protected int layoutResID() {
        return R.layout.fragment_car_list_;
    }

    @Override
    protected void initData() {
        uid= SPUtils.getString(context, ConstantValue.SpType.userid);
        newtoken= SPUtils.getString(context,ConstantValue.SpType.newtoken);

        userPresenter = new UserPresenter(this);

        //adapter
        adapter = new CarAdapter(context,list);
        //manager
        pullToRefreshListView.setAdapter(adapter);

        pullToRefreshListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                //用车牌号去换取url
                CarModel model=(CarModel) adapterView.getAdapter().getItem(i);
                getYuCheBaoSubscribeUrl(model.getVehicleId());

            }
        });
        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                page=0;
                list.clear();
                getCars(page,pagesize,showAll);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page++;
                getCars(page,pagesize,showAll);
            }
        });

        getCars(page,pagesize,showAll);
    }

    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {

        if(actionType.equals(ConstantValue.RequestKey.getCars)){
            list.addAll((ArrayList<CarModel>)data);
            adapter.notifyDataSetChanged();
            pullToRefreshListView.onRefreshComplete();
        }
        if(actionType.equals(ConstantValue.RequestKey.getYuCheBaoSubscribeUrl)){
            UrlItemModel urlItemModel=(UrlItemModel)data;
            Intent intent =new Intent(context,Html5Activity.class);
            intent.putExtra("url",urlItemModel.getUrl());
            startActivity(intent);
        }

    }

    @Override
    public void onFailed(String code, String msg) {
        pullToRefreshListView.onRefreshComplete();
    }


    public void getCars(int page,int pageSize,boolean showAll){
        userPresenter.getCars(page,pageSize, showAll,ConstantValue.RequestKey.getCars);
    }

    public void getYuCheBaoSubscribeUrl(String vehicleId){
        userPresenter.getYuCheBaoSubscribeUrl(vehicleId,ConstantValue.RequestKey.getYuCheBaoSubscribeUrl);
    }



}
